const api = require('../../../api/mop_api');
import { toast, log, err, to, back } from '../../../utils/wxapi';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    item: {},
    qrcode: '',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({ id: options.id });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getid();
  },
  async getid() {
    try {
      const { id } = this.data;
      const res = await api.board_get({ id });
      console.log(res);
      if (res.respCode === 200) {
        let item = JSON.parse(res.source);
        log(item);
        this.setData({ item });
        this.base(item.qrCode);
      }
    } catch (error) {
      err(error);
      throw error;
    }
  },
  base(e) {
    let str = e.substring(0, 5);
    let qrcode;
    if (str === 'cloud') {
      qrcode = e;
    } else {
      qrcode = `data:image/png;base64,${e}`;
    }
    this.setData({ qrcode });
  },
  handlePreview() {
    wx.previewImage({
      urls: [this.data.qrcode],
      current: this.data.qrcode,
    });
  },
  async del() {
    try {
      const { id } = this.data;
      const res = await api.board_del({ id });
      if (res.respCode === 200) {
        toast('删除成功');
        back();
      } else {
        toast(res.msg);
      }
    } catch (error) {
      err(error);
      throw error;
    }
  },
  edit() {
    to('/dj/pages/addboard/addboard?id=' + this.data.id);
  },
});
